const { Controller } = require('egg');
const path = require('path');


class UploadController extends Controller {
    async uploadFiles() {
        const { ctx, service } = this;
        console.log(ctx.request.body);
        console.log('got %d files', ctx.request.files.length);

        let resp = ''
        try {
            // 遍历处理多个文件
            for (const file of ctx.request.files) {
                console.log('field: ' + file.fieldname);
                console.log('filename: ' + file.filename);
                console.log('encoding: ' + file.encoding);
                console.log('mime: ' + file.mime);
                console.log('tmp filepath: ' + file.filepath);
                let dstPath = path.join(this.config.baseDir, 'app/public', file.filename);
                // 调用服务处理上传文件
                resp = await service.uploadService.handleUploadFile(file.filepath, dstPath);
            }
        } finally {
            // 需要删除临时文件
            await ctx.cleanupRequestFiles();
        }
        ctx.body = resp;
    }
}

module.exports = UploadController;